package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.Rights;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    public String login(User user){
        //获取传入user中的属性
        String password=user.getPassword();
        //加密算法，md5hash相对于md5更加安全
        String md5Pass=DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pass);
        //条件构造器
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>(user);
        //获取数据库中的user
        User userDB=userMapper.selectOne(queryWrapper);
        if(userDB==null){
            return null;
        }
        //去除UUID里面的“-”号，（用空字符串替换）
        String token= UUID.randomUUID().toString().replace("-", "");
        return token;
    }

      /*
      * select*from user limit 0,20    前20条
      *select*from user limit 20,20     第二页20条
      *select*from user limit 40,20
      * select*from user where name like "%#{query}%" limit 40,20
      * */
    @Override
    public PageResult getUserList(PageResult pageResult) {
        //
        IPage<User> page=new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        boolean flag= StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"username", pageResult.getQuery());
        //page参数，分页，queryWrapper参数，where条件
        page=userMapper.selectPage(page,queryWrapper);
        long total=page.getTotal();
        List<User> list=page.getRecords();
        return pageResult.setRows(list).setTotal(total);
    }

    @Override
    @Transactional
    public void updateStatus(User user) {

        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    @Transactional//事务，运行期异常，编译期异常（检查异常），遇到异常回滚
    public void addUser(User user) {
        String md5Pass=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        userMapper.insert(user);
    }

    @Override
    public User getUserById(Integer id) {

        return userMapper.selectById(id);
    }

    //upadate user set p....,e where id=...
    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }


}
